// 同步数据存储
const setStorage = (key, data) => {
  try {
    wx.setStorageSync(key, data)
  } catch (error) {
    console.error(`存储指定${key}数据发生了异常`, error)
  }
}

// 同步数据获取
const getStorage = (key) => {
  try {
    const value = wx.getStorageSync(key)
    if (value) {
      return value
    }
  } catch (error) {
    console.error(`读取指定${key}数据发生了异常`, error)
  }
}

// 同步数据移除
const removeStorage = (key) => {
  try {
    wx.removeStorageSync(key)
  } catch (error) {
    console.error(`移除指定${key}数据发生了异常`, error)
  }
}

// 同步数据清空
const clearStorage = () => {
  try {
    wx.clearStorageSync()
  } catch (error) {
    console.error(`清空、清除数据发生了异常`, error)
  }
}

// 异步数据存储
const aysncSetStorage = (key, data) => {
  return new Promise((resolve) => {
    wx.setStorage({
      key,
      data,
      complete(res) {
        resolve(res)
      }
    })
  })
}

// 异步数据获取
const aysncGetStorage = (key) => {
  return new Promise((resolve, reject) => {
    wx.getStorage({
      key,
      success(res) {
        resolve(res)
      },
      error(err) {
        reject(err)
      }
    })
  })
}

// 异步数据移除
const aysncRemoveStorage = (key) => {
  return new Promise((resolve, reject) => {
    wx.removeStorage({
      key,
      success(res) {
        resolve(res)
      },
      error(err) {
        reject(err)
      }
    })
  })
}

// 异步数据清空
const aysncClearStorage = () => {
  return new Promise((resolve, reject) => {
    wx.clearStorage({
      success(res) {
        resolve(res)
      },
      error(err) {
        reject(err)
      }
    })
  })
}

export {
  setStorage,
  getStorage,
  removeStorage,
  clearStorage,
  aysncSetStorage,
  aysncGetStorage,
  aysncRemoveStorage,
  aysncClearStorage
}









